package com.ysm.smfu.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.ysm.smfu.R;
import com.ysm.smfu.utils.UIUtils;

/**
 * Created by Administrator on 2016/10/25.
 */
public class RightToTopDelView extends RelativeLayout {

    private ImageView img;
    private ImageView img_del;
    private DelImg delImg;
    private ScaleAnimation animation;
    private ScaleAnimation animation2;

    public RightToTopDelView(Context context) {
        this(context, null);
    }

    public RightToTopDelView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RightToTopDelView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }


    /**
     * 初始化布局
     */
    private void initView() {
        animation = new ScaleAnimation(1f, 1.2f, 1f, 1.2f, 0.5f, 0.5f);
        animation.setDuration(200);
        animation.setFillAfter(true);

        animation2 = new ScaleAnimation(1.2f, 1f, 1.2f, 1f, 0.5f, 0.5f);
        animation.setDuration(200);
        animation.setFillAfter(true);


        View view = UIUtils.inflate(R.layout.view_righttotop);
        addView(view);
        img = (ImageView) view.findViewById(R.id.img);
        img_del = (ImageView) view.findViewById(R.id.img_del);
        img.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (img_del.getVisibility() == View.VISIBLE) {
                    img.setColorFilter(null);
                    img_del.setVisibility(GONE);
                    img.startAnimation(animation2);
                } else if (img_del.getVisibility() == View.GONE) {
                    img_del.setVisibility(VISIBLE);
                    img.setColorFilter(Color.parseColor("#77000000"));
                    img.startAnimation(animation);
                }
                return true;
            }
        });
        img_del.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                img_del.setVisibility(GONE);
                if (delImg != null)
                    delImg.delImg();
            }
        });
        img.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (img_del.getVisibility() == View.VISIBLE) {
                    img.setColorFilter(null);
                    img_del.setVisibility(GONE);
                    img.startAnimation(animation2);
                }
            }
        });
    }

    public void setOnDelImg(DelImg delImg) {
        this.delImg = delImg;
    }

    public interface DelImg {
        public void delImg();
    }

    public ImageView getImageView() {
        return img;
    }

    //    设置图片
    public void setImageBitmap(Bitmap bitmap) {
        img.setImageBitmap(bitmap);
    }

}
